package com.hsj.demo.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hsj.demo.dao.ChatHistoryDao;
import com.hsj.demo.entity.ChatHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class ChatHistoryService extends CurdService<ChatHistoryDao, ChatHistory> {
    @Autowired
    private ChatHistoryDao chatHistoryDao;
    @Autowired
    private UserFriendService userFriendService;

    // 查询该用户和目标用户的聊天记录
    public List<ChatHistory> findByTime(Integer sourceUserId, Integer targetUserId){
        return chatHistoryDao.findByTime(sourceUserId,targetUserId);
    }

    public String update(Integer id,Character isRead) {
        chatHistoryDao.update(id,isRead);
        return "修改完成";
    }

    public String updateIsReadList(List<ChatHistory> isReadList){
        for(ChatHistory chatHistory:isReadList){
            chatHistoryDao.update(chatHistory.getId(),chatHistory.getIsRead());
        }
        return "修改完成";
    }

    public ChatHistory insert(HashMap hashMap) {
        ChatHistory chatHistory = new ObjectMapper().convertValue(hashMap.get("chatHistory"),ChatHistory.class);
        chatHistoryDao.insert(chatHistory);
        return chatHistory;
    }
}
